package com.cdplife.ci.modular.system.controller;

import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.cdplife.ci.core.common.annotion.BussinessLog;
import com.cdplife.ci.core.common.annotion.FormToken;
import com.cdplife.ci.core.common.annotion.Permission;
import com.cdplife.ci.core.common.constant.dictmap.InsuranceKindDict;
import com.cdplife.ci.core.common.constant.dictmap.PolicyDict;
import com.cdplife.ci.core.common.exception.BizExceptionEnum;
import com.cdplife.ci.core.shiro.ShiroKit;
import com.cdplife.ci.modular.system.warpper.InsuranceKindWarpper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;
import com.cdplife.ci.core.log.LogObjectHolder;
import org.springframework.web.bind.annotation.RequestParam;
import com.cdplife.ci.modular.system.model.InsuranceKind;
import com.cdplife.ci.modular.system.service.IInsuranceKindService;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 控制器
 *
 * @author fengshuonan
 * @Date 2018-11-07 14:26:56
 */
@Controller
@RequestMapping("/insuranceKind")
public class InsuranceKindController extends BaseController {

    private String PREFIX = "/system/insuranceKind/";

    @Autowired
    private IInsuranceKindService insuranceKindService;

    /**
     * 跳转到首页
     */
    @RequestMapping("/{policyId}")
    public String index(@PathVariable Integer policyId, Model model) {
        model.addAttribute("policyId",policyId);
        return PREFIX + "insuranceKind.html";
    }

    /**
     * 跳转到添加
     */
    @FormToken(save=true)
    @RequestMapping("/insuranceKind_add/{policyId}")
    public String insuranceKindAdd(@PathVariable Integer policyId, Model model) {
        model.addAttribute("policyId",policyId);
        return PREFIX + "insuranceKind_add.html";
    }

    /**
     * 跳转到修改
     */
    @FormToken(save=true)
    @RequestMapping("/insuranceKind_update/{insuranceKindId}/{status}")
    public String insuranceKindUpdate(@PathVariable Integer insuranceKindId,@PathVariable Integer status, Model model) {
        InsuranceKind insuranceKind = insuranceKindService.selectById(insuranceKindId);
        model.addAttribute("item",insuranceKind);
        model.addAttribute("status",status);
        LogObjectHolder.me().set(insuranceKind);
        return PREFIX + "insuranceKind_edit.html";
    }

    /**
     * 获取列表
     */
    @RequestMapping(value = "/list/{policyId}")
    @ResponseBody
    public Object list(@PathVariable("policyId") Integer policyId) {
        List<Map<String, Object>> result = insuranceKindService.selectInsuranceKindList(policyId);
        return new InsuranceKindWarpper(result).wrap();
    }

    /**
     * 新增
     */
    @FormToken(remove=true)
    @BussinessLog(value = "添加险种", key = "policyId,policyPlanCode", dict = InsuranceKindDict.class)
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(InsuranceKind insuranceKind) {
        return insuranceKindService.addInsuranceKind(insuranceKind);
    }

    /**
     * 删除
     */
    @FormToken(remove=true)
    @BussinessLog(value = "删除险种", key = "insuranceKindId", dict = InsuranceKindDict.class)
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer insuranceKindId) {
        return insuranceKindService.deleteInsuranceKind(insuranceKindId);
    }

    /**
     * 修改
     */
    @FormToken(remove=true)
    @BussinessLog(value = "修改险种", key = "policyId,insuranceKindName", dict = InsuranceKindDict.class)
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(InsuranceKind insuranceKind) {
        return insuranceKindService.updateInsuranceKind(insuranceKind);
    }

    /**
     * 详情
     */
    @RequestMapping(value = "/detail/{insuranceKindId}")
    @ResponseBody
    public Object detail(@PathVariable("insuranceKindId") Integer insuranceKindId) {
        return insuranceKindService.selectById(insuranceKindId);
    }
}
